﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


namespace TravelLink.Common {

    public static class PhotoUtils {

        // *** Jpeg Format ***
        private static ImageCodecInfo GetEncoderInfo(string mimeType) {
            int j;
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j) {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        public static void SaveToJpeg(Bitmap bitmap, string filePath, long qualityPercent) {
            ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/jpeg");
            Encoder encoder = Encoder.Quality;
            EncoderParameters encoderParameters = new EncoderParameters(1);
            encoderParameters.Param[0] = new EncoderParameter(encoder, qualityPercent);
            bitmap.Save(filePath, imageCodecInfo, encoderParameters);
        }

        // *** Thumbnails ***
        public static Bitmap CreateThumbnail(Image originalImage, int thumbnailSize) {



            int height = thumbnailSize;
            int width = thumbnailSize;

            int towidth = thumbnailSize;
            int toheight = thumbnailSize;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;       //源图片宽   
            int oh = originalImage.Height;      //源图片高   


            double wh = towidth / toheight;
            double owh = ow / oh;

            //按等比宽计算生成图片的宽高   
            toheight = oh * towidth / ow;       //得到按宽等比的高   

            if (toheight > height) //如果等比的高还是大于生成缩略图的高   
                {
                towidth = towidth * height / toheight;
                toheight = height;
            }
            //新建一个bmp图片   
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板   
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.Clear(System.Drawing.Color.Transparent);
                //在指定位置并且按指定大小绘制原图片的指定部分   
                g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);

            }

            return bitmap;




            //int w;
            //int h;
            //if (image.Width > image.Height) {
            //    h = thumbnailSize;
            //    w = (int)((image.Width * (float)h) / (float)image.Height);
            //}
            //else {
            //    w = thumbnailSize;
            //    h = (int)((image.Height * (float)w) / (float)image.Width);
            //}
            //Bitmap newImage = new Bitmap(thumbnailSize, thumbnailSize, PixelFormat.Format24bppRgb);
            //using (Graphics canvas = Graphics.FromImage(newImage)) {
            //    canvas.SmoothingMode = SmoothingMode.AntiAlias;
            //    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //    canvas.DrawImage(image, new Rectangle(0, 0, w, h));
            //}
            //return newImage;
        }

        public static void CreateThumbnailFile(string filePath, int thumbnailSize,
            string thumbnailFilePath, long qualityPercent) {

            using (FileStream stream = new FileStream(filePath, FileMode.Open)) {
                using (System.Drawing.Image image =
                    System.Drawing.Image.FromStream(stream)) {

                    if (image.Width == thumbnailSize && image.Height == thumbnailSize) {
                        File.Copy(filePath, thumbnailFilePath, true);
                    }
                    else {
                        using (Bitmap bitmap =
                            CreateThumbnail(image, thumbnailSize)) {
                            SaveToJpeg(bitmap, thumbnailFilePath, qualityPercent);
                        }
                    }
                }
            }
        }

        public static void CreateThumbnailFile(string filePath, int thumbnailSize,
            string thumbnailFilePath) {
            CreateThumbnailFile(filePath, thumbnailSize, thumbnailFilePath, 90L);
        }
    }
}
